package com.gxgy.controller;


import java.util.List;

import javax.servlet.http.HttpSession;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gxgy.base.BaseAllInterface;
import com.gxgy.entity.Role;
import com.gxgy.entity.User;
import com.gxgy.entity.dto.UserDto;
import com.gxgy.service.UserService;
import com.gxgy.uilt.IfVersionException;
@Controller
public class RegisterController extends BaseAllInterface{
	
	@RequestMapping("/register")
	public String register(Model model) {
		List<Role> list = roleService.queryAllRole();
		model.addAttribute("role",list);
		return "page/register";
		
	}
	@RequestMapping("/registerInfo")
	public String echoRegisterInfo(String name,String username,String sex,
			String status,String password,String confirmPassword,
			String date,Model model) {
		System.out.println("===========================>"+name);
		System.out.println("===========================>"+username);
		System.out.println("===========================>"+sex);
		System.out.println("===========================>"+status);
		System.out.println("===========================>"+password);
		System.out.println("===========================>"+confirmPassword);
		System.out.println("===========================>"+date);
		User userByName =  userService.queryUserByUserName(username);
		if (userByName != null) {
			model.addAttribute("msg","该用户名已存在");
			return "redirect:/register";

		}
		if (!confirmPassword.equals(password)) {
			model.addAttribute("msg","密码不一致");
			return "redirect:/register";
		}
		Role roleByName = roleService.queryRoleByName(status);
		if (roleByName == null) {
			model.addAttribute("msg","该身份不存在");
			return "redirect:/register";
		}
		UserDto dto = new UserDto();
		dto.setName(name);
		dto.setUsername(username);
		dto.setSex(sex);
		if (status.equals(roleByName.getRoleName())) {
			dto.setStatus(status);
		}else {
			return "page/404";
		}

		dto.setPassword(password);
		dto.setConfirmPassword(confirmPassword);
		dto.setDate(date);
		model.addAttribute("user",dto);
		
		return "page/register-02";
		
	}
	
	@RequestMapping("/doRegister")
	public String doRegister(String name,String username,String sex,
			String status,String password,
			String date,Model model) {
		System.out.println("===================>"+name);
		System.out.println("===================>"+username);
		System.out.println("===================>"+sex);
		System.out.println("===================>"+status);
		System.out.println("===================>"+password);
		if (name.isEmpty()) {
			model.addAttribute("msg","昵称不能为空");
			throw new IfVersionException("name不能为空!");
		}
		if (username.isEmpty()) {
			model.addAttribute("msg","用户名不能为空");
			throw new IfVersionException("username不能为空!");
		}
		if (sex.isEmpty()) {
			model.addAttribute("msg","性别不能为空");
			throw new IfVersionException("性别不能为空！");
		}
		if (password.isEmpty()) {
			model.addAttribute("msg","密码不能为空");
			throw new IfVersionException("password不能为空!");
		}
		if (status.isEmpty()) {
			model.addAttribute("msg","身份不能为空");
			throw new IfVersionException("角色名称不能为空");
		}
		if (date.isEmpty()) {
			model.addAttribute("msg","注册时间不能为空");
			throw new IfVersionException("注册时间不能为空");
		}
		
		User user = userService.queryUserByUserName(username);
		if (user != null) {
			model.addAttribute("msg","该用户名已存在");
			throw new IfVersionException("usename已存在，该用户名已存在");
		}
		
		Role role = roleService.queryRoleByName(status);
		if (role == null) {
			model.addAttribute("msg","该身份不存在");
			throw new IfVersionException("该角色不存在");
		}
		userService.addOneUser(name, username, sex, password, status, date,model);
		System.out.println("===================>添加成功");
		return "page/login";
		
	}
	

}
